package com.piggy.logi.sys.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.piggy.common.core.utils.PageUtils;
import com.piggy.common.core.utils.sql.SqlUtil;
import com.piggy.common.core.web.page.PagePlus;
import com.piggy.common.core.web.page.TableDataInfo;
import com.piggy.common.satoken.utils.SecurityUtils;
import com.piggy.logi.sys.api.domain.LogiSysDept;
import com.piggy.logi.sys.bo.LogiKefuFineEditBo;
import com.piggy.logi.sys.bo.LogiKefuFineQueryBo;
import com.piggy.logi.sys.domain.LogiKefuFine;
import com.piggy.logi.sys.mapper.LogiKefuFineMapper;
import com.piggy.logi.sys.service.ILogiCustomerService;
import com.piggy.logi.sys.service.ILogiKefuFineService;
import com.piggy.logi.sys.service.ILogiSysDeptService;
import com.piggy.logi.sys.service.ILogiSysUserService;
import com.piggy.logi.sys.vo.LogiKefuFineExtVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 罚款问题登记Service业务层处理
 *
 * @author piggy
 * @date 2023-10-25
 */
@Slf4j
@Service
public class LogiKefuFineServiceImpl extends ServiceImpl<LogiKefuFineMapper, LogiKefuFine> implements ILogiKefuFineService {

    @Resource
    private ILogiCustomerService customerService;

    @Resource
    private ILogiSysUserService userService;

    @Resource
    private ILogiSysDeptService deptService;

    @Override
    public LogiKefuFineExtVo queryById(Long id) {
        return fillData(getVoById(id, LogiKefuFineExtVo.class));
    }

    @Override
    public TableDataInfo<LogiKefuFineExtVo> queryPageList(LogiKefuFineQueryBo bo) {
        PagePlus<LogiKefuFine, LogiKefuFineExtVo> result = pageVo(PageUtils.buildPagePlus(bo, true), buildQueryWrapper(bo), LogiKefuFineExtVo.class);
        result.setRecordsVo(fillData(result.getRecordsVo()));
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<LogiKefuFineExtVo> queryList(LogiKefuFineQueryBo bo) {
        return fillData(listVo(buildQueryWrapper(bo), LogiKefuFineExtVo.class));
    }

    private LambdaQueryWrapper<LogiKefuFine> buildQueryWrapper(LogiKefuFineQueryBo bo) {
        LambdaQueryWrapper<LogiKefuFine> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getId() != null, LogiKefuFine::getId, bo.getId());
        //lqw.eq(bo.getDeptId() != null, LogiKefuFine::getDeptId, bo.getDeptId());
        lqw.eq(bo.getFineType() != null, LogiKefuFine::getFineType, bo.getFineType());
        lqw.eq(bo.getFineAmt() != null, LogiKefuFine::getFineAmt, bo.getFineAmt());
        lqw.eq(bo.getOccurTime() != null, LogiKefuFine::getOccurTime, bo.getOccurTime());
        lqw.eq(bo.getWaybillId() != null, LogiKefuFine::getWaybillId, bo.getWaybillId());
        lqw.eq(StrUtil.isNotBlank(bo.getFineCode()), LogiKefuFine::getFineCode, bo.getFineCode());
        lqw.eq(StrUtil.isNotBlank(bo.getOutCode()), LogiKefuFine::getOutCode, bo.getOutCode());
        lqw.eq(StrUtil.isNotBlank(bo.getScheduleNo()), LogiKefuFine::getScheduleNo, bo.getScheduleNo());
        lqw.eq(StrUtil.isNotBlank(bo.getBusNo()), LogiKefuFine::getBusNo, bo.getBusNo());
        lqw.eq(bo.getFinerType() != null, LogiKefuFine::getFinerType, bo.getFinerType());
        lqw.eq(StrUtil.isNotBlank(bo.getFiner()), LogiKefuFine::getFiner, bo.getFiner());
        lqw.eq(bo.getPayeeType() != null, LogiKefuFine::getPayeeType, bo.getPayeeType());
        lqw.eq(StrUtil.isNotBlank(bo.getPayee()), LogiKefuFine::getPayee, bo.getPayee());
        lqw.apply(ObjectUtil.isAllNotEmpty(bo.getCreateTimeStart(), bo.getCreateTimeEnd()), "date_format(create_time,'%y%m%d') between date_format({0},'%y%m%d') and date_format({1},'%y%m%d') " , bo.getCreateTimeStart(), bo.getCreateTimeEnd());
        // 部门数据权限过滤
        if (ObjectUtil.isNotNull(bo.getDeptId())) {
            SqlUtil.dataPermissionFilter(lqw, bo.getDeptId());
        }
        return lqw;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public LogiKefuFineExtVo insertByAddBo(LogiKefuFineEditBo bo) {
        /*if (StrUtil.isBlank(bo.getFineCode())) {
            String date = DateUtil.format(new Date(), "yyyyMMdd");
            String key = CacheKeyEnums.LogiFineNo.getKey(date);
            if (!CacheUtils.hasKey(key)) {
                CacheUtils.cache(0, key, 2L, TimeUnit.DAYS);
            }
            Long no = CacheUtils.incrBy(key);
            bo.setFineCode(String.format("%s%s%06d", BizNoPrefixEnum.FINE_NO.getCode(), date, no));
        }*/
        LogiKefuFine add = BeanUtil.toBean(bo, LogiKefuFine.class);
        add.setDeptId(SecurityUtils.getMainDeptId());
        add.setBranchDeptId(SecurityUtils.getDeptId());
        save(add);
        return queryById(add.getId());
    }

    @Override
    @Transactional
    public LogiKefuFineExtVo updateByEditBo(LogiKefuFineEditBo bo) {
        LogiKefuFine update = BeanUtil.toBean(bo, LogiKefuFine.class);
        updateById(update);
        return queryById(update.getId());
    }

    @Override
    @Transactional
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }

    private LogiKefuFineExtVo fillData(LogiKefuFineExtVo Any) {

        if (ObjectUtil.isNotEmpty(Any.getBranchDeptId())) {
            LogiSysDept dept = deptService.getById(Any.getBranchDeptId());
            if (ObjectUtil.isNotEmpty(dept)) {
                Any.setBranchDeptName(dept.getDeptName());
            }
        }

        return Any;
    }

    private List<LogiKefuFineExtVo> fillData(List<LogiKefuFineExtVo> voList) {
        voList.stream().peek(this::fillData).collect(Collectors.toList());
        return voList;
    }
}
